from order.models import Order_form
from account.models import Personnel
from results.models import Day_money,Month_money,Years_money
import datetime


def count_day():
    orders = Order_form.objects.filter(times__gt=datetime.datetime.now() - datetime.timedelta(hours=23, minutes=59, seconds=59)).all()
    money = 0
    for order in orders:
        money += order.order_money
    daymoney = Day_money()
    daymoney.day_money = money
    daymoney.save()



def month_money():
    personnels = Personnel.objects.all()

    current_month = int(str(datetime.datetime.now()).split("-")[1])
    orders = Order_form.objects.filter(times__month=current_month-1).all()
    days = Day_money.objects.filter(day_time__month=current_month-1).all()
    money = 0
    for day in days:
        money += day.day_money
    month_money = Month_money()
    month_money.month_money = money
    month_money.save()
    for personnel in personnels:
        for order in orders:
            if order.order_washhair_name == personnel:
                personnel.commission += 2
            if order.order_havehaircut_name == personnel:
                personnel.commission += 2
        personnel.wage = personnel.commission + personnel.base_salary
        personnel.save()


def years_money():
    current_year = int(str(datetime.datetime.now()).split("-")[0])
    months = Month_money.objects.filter(month_time__year=current_year-1).all()
    money = 0
    for month in months:
        money += month.month_money
    years_money = Years_money()
    years_money.years_money = money
    years_money.save()